package com.opennews.openplatform.familyexpenseapi.controller

import com.opennews.openplatform.familyexpenseapi.dto.account.group.user.QuitAccountGroupUserDto
import com.opennews.openplatform.familyexpenseapi.dto.account.group.user.RemoveAccountGroupUserDto
import com.opennews.openplatform.familyexpenseapi.service.AccountGroupUserService
import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import com.opennews.openplatform.myspringbootcore.util.getSuccessResponse
import com.opennews.openplatform.myspringbootmvccore.security.RequestIdentityExtractor
import jakarta.validation.Valid
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/account-group-user")
class AccountGroupUserController(
    private val accountGroupUserService: AccountGroupUserService,
    private val requestIdentityExtractor: RequestIdentityExtractor,
) {
    @GetMapping("/query-available-account-groups")
    fun queryAvailableAccountGroups(): ResponseEntity<ApiResponse<List<Map<String, Any?>>>> {
        val claims = requestIdentityExtractor.getClaims()
        val userId = claims.id.asString()
        val result = accountGroupUserService.queryAvailableAccountGroups(userId)

        return getSuccessResponse(result)
    }

    @PostMapping("/remove")
    fun remove(@Valid @RequestBody data: RemoveAccountGroupUserDto): ResponseEntity<ApiResponse<Any?>> {
        val claims = requestIdentityExtractor.getClaims()
        val accountGroupId = claims.accountGroupId.asString()
        val userId = data.userId
        val username = data.username
        accountGroupUserService.remove(accountGroupId, userId, username)

        return getSuccessResponse()
    }

    @PostMapping("/quit")
    fun quit(@Valid @RequestBody data: QuitAccountGroupUserDto): ResponseEntity<ApiResponse<Any?>> {
        val accountGroupId = data.accountGroupId
        val claims = requestIdentityExtractor.getClaims()
        val userId = claims.id.asString()
        val username = claims.username
        accountGroupUserService.remove(accountGroupId, userId, username)

        return getSuccessResponse()
    }
}